<template>
	<view class="cardstyle" :style="{'background-color':bgColor}">
		<view class="titlebox">
			<view class="imgbox" v-if="mode==2">
				<image :src="showImage" @error="handleImageException()"></image>
			</view>
			<view>
			<text v-if="mode==3" style="color: #aaa;">{{title}}</text>
			<text v-else>{{title}}</text>
			</view>
		</view>
		<view class="adbox" v-if="mode==3" style="margin-top: 5rpx;">
			<view v-for="(item,index) in images" :key="index">
				<image :src="item" mode="aspectFill" @error="handleImageError(index)"></image>
			</view>
		</view>
		<view class="tipsbox">
			<text v-if="isTop" style="color: #f00; margin-right: 10rpx;">置顶</text>
			<text v-if="mode==3" style="color: #00f; margin-right: 10rpx;">广告</text>
			<text style="margin-right: 10rpx;">{{author}}</text>
			<text v-if="mode!=3" style="margin-right: 10rpx;">{{comments}}评</text>
			<view style="flex: 1; display: flex; justify-content: flex-end;">
				<text>{{time}}</text>
			</view>
		</view>
		<slot name="tips" v-if="showSearch"></slot>
	</view>
</template>

<script>
	export default {
		name: "cardViewText",
		props: {
			images: {
				type: Array,
				default: () => []
			},
			nID: {
				type: String,
				default:"0",
				required: true
			},
			title: {
				type: String,
				default:"新闻标题",
				required: true
			},
			isTop: {
				type: Boolean,
				default:false,
				required: true
			},
			author: {
				type: String,
				default:"来源",
				required: true
			},
			comments: {
				type: String,
				default:"0"
			},
			time: {
				type: String,
				default:""
			},
			mode: {
				type: Number,
				default: 1,
				required:true
			},
			showSearch:{
				type:Boolean,
				default:false
			},
			bgColor:{
				type:String,
				default:'#fff'
			}
		},
		computed: {
		  showImage() {
		    // 单图模式（mode=2）取images数组第一个元素
		    const imgSrc = this.images[0];
		    if (imgSrc) {
		      return this.imageError ? this.failImage : imgSrc;
		    } else {
		      console.log("showImage defaultImage: " + this.title);
		      return this.defaultImage;
		    }
		  }
		},
		data() {
			return {
				defaultImage: "/static/newsimage/load_default.png",
				failImage: "/static/newsimage/load_fail.png",
				imageError: false,
				newsList:[]
			};
		},
		methods: {
			handleImageException(){
				console.log("handleImageException"+this.title);
				this.imageError = true;
			},
			handleImageError(index) {
				console.log("handlerImageError----->:" + this.title);
				console.log("handlerImageError----->index:" +index);
				this.$set(this.images,index,this.failImage);
			}
		}
	}
</script>

<style>
	.adbox{
		display: flex;
		flex-direction: row;
		justify-content: space-around;
		gap: 8rpx;
	}
	.adbox view{
		flex: 1;
		height: 120rpx;
		overflow: hidden;
		border-radius: 4rpx;
	}
	.adbox image{
		width: 100%;
		height: 100%;
		object-fit: cover;
	}
	.cardstyle {
		background-color: #fff;
		padding: 10rpx;
		border-radius:4rpx;
		margin-bottom: 10rpx;
		box-shadow: 0 1rpx 2rpx rgba(0, 0, 0, 0.1);
	}
	.titlebox {
		display: flex;
		align-items: flex-start;
		gap: 10rpx;
		margin-bottom: 8rpx;
	}
	.imgbox{
		width: 80rpx;
		height: 80rpx;
		flex-shrink: 0;
		overflow: hidden;
		border-radius: 4rpx;
	}
	.imgbox image{
		width: 100%;
		height: 100%;
		object-fit: cover;
	}
	.tipsbox {
		color: #999;
		font-size: 12rpx;
		margin-top: 5rpx;
		display: flex;
		align-items: center;
	}
</style>